﻿//获取当天比周一多了几天
function getMonday() {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var date = d.getDate();
// 周
    var day = d.getDay();
    var monday = day != 0 ? day - 1 : 6; // 本周一与当前日期相差的天数
    return monday;
}

//获取当月所属季度的开始月份
function getQuarterStartMonth(month) {
    var spring = 1; //春
    var summer = 4; //夏
    var fall = 7;   //秋
    var winter = 10; //冬
    if (month < 3) {
        return spring;
    }
    if (month < 6) {
        return summer;
    }
    if (month < 9) {
        return fall;
    }
    return winter;
};

//月份或天为个位数的时候补零，不补的话很多浏览器在使用new Date（）把string转date的时候无法识别
function addZero(i) {
    if (parseInt(i) < 10) {
        return "0" + i;
    } else {
        return i;
    }
}

jQuery.dates = {
    //获取与当前天数相隔几天后的日期
    getDate: function (dateDiff, format) {
        var dd = new Date();
        dd.setDate(dd.getDate() + dateDiff);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1; //从0开始,所以需要+1
        var d = dd.getDate();
        var day = y + "-" + addZero(m) + "-" + addZero(d);
        if (format == "yyyy/MM/dd") {
            day = day.replace(/-/g, "/");
        }
        return day;
    },
    //获取与当前天数相隔几周后的日期,type="s"为开始日期,"e"为结束日期
    getWeekDate: function (type, weekDiff, format) {
        var dd = new Date();
        var sday = 7 * weekDiff;
        var eday = sday + 6;
        var mondayDatys = getMonday();
        if (type == "s") {
            dd.setDate(dd.getDate() + sday - mondayDatys);
        } else {
            dd.setDate(dd.getDate() + eday - mondayDatys);
        }
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1; //从0开始,所以需要+1
        var d = dd.getDate();
        var day = y + "-" + addZero(m) + "-" + addZero(d);
        if (format == "yyyy-MM-dd") {
            day = day.replace(/-/g, "/");
        }
        return day;
    },
    //获取与当前天数相隔几月后的日期,type="s"为开始日期,"e"为结束日期
    getMonthDate: function (type, monthDiff, format) {
        var d = new Date();
        var year = d.getFullYear();
        var month = d.getMonth() + 1;
        if (monthDiff != 0) {
            // 如果本月为12月，年份加1，月份为monthDiff，否则月份加1。
            if (month == 12 && monthDiff > 0) {
                year++;
                month = monthDiff;
            } else if (month == 1 && monthDiff < 0) {
                year--;
                month = 12 + monthDiff + 1;
            } else {
                month = month + monthDiff;
            }
        }
        var date = d.getDate();
        var firstday = year + "-" + month + "-" + 1;
        var lastday = "";
        var lastDay;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            lastday = year + "-" + month + "-" + 31;
        } else if (month == 2) {
            // 判断是否为闰年（能被4整除且不能被100整除 或 能被100整除且能被400整除）
            if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
                lastday = year + "-" + month + "-" + 29;
                lastDay = 29;
            } else {
                lastday = year + "-" + month + "-" + 28;
                lastDay = 28;
            }
        } else {
            lastday = year + "-" + month + "-" + 30;
            lastDay = 30;
        }
        var day = "";
        if (type == "s") {
            day = firstday;
        } else if (type == "n") {
            if (date > lastDay) {
                date = lastDay;
            }
            day = year + "-" + month + "-" + date;
        } else {
            day = lastday;
        }
        if (format == "yyyy-MM-dd") {
            day = day.replace(/-/g, "/");
        }

        return day;
    },
    //相隔几个季度后的日期
    getQuarterDate: function (type, QuarterDiff, format) {
        var d = new Date();
        var year = d.getFullYear();
        var month = d.getMonth() + 1;
        var quaStmonth = getQuarterStartMonth(month);
        var months = QuarterDiff * 3;
        if (months != 0) {
            if (quaStmonth == 10 && months > 0) {
                year++;
                quaStmonth = 1;
            } else if (quaStmonth == 1 && months < 0) {
                year--;
                quaStmonth = 10;
            } else {
                quaStmonth = quaStmonth + months;
            }
        }

        var fd = year + "-" + quaStmonth + "-" + 1;
        var ld = "";
        if (quaStmonth == 1 || quaStmonth == 10) {
            ld = year + "-" + (quaStmonth + 2) + "-" + 31;
        } else {
            ld = year + "-" + (quaStmonth + 2) + "-" + 30;
        }

        var quaDate = "";
        if (type == "s") {
            quaDate = fd;
        } else {
            quaDate = ld;
        }
        if (format == "yyyy/MM/dd") {
            quaDate = quaDate.replace(/-/g, "/");
        }
        return quaDate;
    },
    //相隔几年后的日期
    getYearDate: function (type, years, format) {
        var d = new Date();
        var year = d.getFullYear();

        var fd = (year + years) + "-" + 01 + "-" + 01;
        var ld = (year + years) + "-" + 12 + "-" + 31;

        var yr = "";
        if (type == "s") {
            yr = fd;
        } else {
            yr = ld;
        }
        if (format == "yyyy/MM/dd") {
            quaDate = quaDate.replace(/-/g, "/");
        }
        return yr;
    },
    //把date类型转string
    parseDateToString: function (format, date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1; //从0开始,所以需要+1
        var d = date.getDate();
        switch (format) {
            case "yyyy/MM/dd":
                return y + "/" + m + "/" + d;
                break;
            default:
                return y + "-" + addZero(m) + "-" + addZero(d);
                break;
        }
    },

    parseMSToString: function (format, ms) {
        var date = new Date(ms);
        var y = date.getFullYear();
        var m = date.getMonth() + 1; //从0开始,所以需要+1
        var d = date.getDate();
        switch (format) {
            case "yyyy/MM/dd":
                return y + "/" + m + "/" + d;
                break;
            default:
                return y + "-" + addZero(m) + "-" + addZero(d);
                break;
        }
    },

    getDayDiff: function (stdate, eddate) {
        var std = new Date(stdate.replace(/-/g, "/"));
        var edd = new Date(eddate.replace(/-/g, "/"));
        var diffDays = parseInt(Math.abs(edd - std) / 1000 / 60 / 60 / 24);
        return diffDays;
    },
    addDate: function (date, dateDiff, format) {
        var dd = new Date(date.replace(/-/g, "/"));
        dd.setDate(dd.getDate() + dateDiff);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1; //从0开始,所以需要+1
        var d = dd.getDate();
        day = y + "-" + addZero(m) + "-" + addZero(d);

        if (format == "yyyy-MM-dd") {
            day = day.replace(/-/g, "/");
        }
        return day;
    },
    parseStringToDate: function (dateString) {
        var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
            date = new Date(NaN), month,
            parts = isoExp.exec(dateString);

        if (parts) {
            month = +parts[2];
            date.setFullYear(parts[1], month - 1, parts[3]);
            if (month != date.getMonth() + 1) {
                date.setTime(NaN);
            }
        }
        return date;
    }
}